﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Formqxpz : Form
    {

        int i = 0;
        int ksrow;
        int jsrow;
        int ksrow2;
        int jsrow2;
        public Formqxpz()
        {
            InitializeComponent();
        }

       

        private void button_search_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("008001"))
            {

                string sql = @"  select '' 行号, Z_UA_User.cuser_name 人员
                            ,Z_UA_User.cuser_id 工号,Z_UserPassword.password 密码 from Z_UA_User
                        left join  Z_UserPassword
                          on Z_UserPassword.cUSER_Id = Z_UA_User.cUSER_Id
                                -- where cdept !='系统组' ";
            System.Data.DataTable tb = pub.U8GetTable(sql);
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["行号"] = (i + 1).ToString();
                   
                }
            }
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = tb;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridView1.Refresh();

            string sql2 = "  select '' 行号, 权限分组 权限分组,权限编号 权限编号,权限名称 权限名称 from Z_gn ";
            System.Data.DataTable tb2 = pub.U8GetTable(sql2);
            if (tb2 != null)
            {
                for (int i = 0; i < tb2.Rows.Count; i++)
                {
                    tb2.Rows[i]["行号"] = (i + 1).ToString();
                    

                }
            }
            this.dataGridView2.AutoGenerateColumns = false;
            this.dataGridView2.DataSource = tb2;
            this.dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridView2.Refresh();


            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
        }


     
       


        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox2.Checked;
               
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
          
            this.progressBar1.Value = 100;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            if (ksrow > jsrow)
            {
                int r;
                r = ksrow;
                ksrow = jsrow;
                jsrow = r;

            }
            for (int i = ksrow; i <= jsrow; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox1.Checked;
               // this.dataGridView2.Rows[i].Cells[1].Value = checkBox1.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;
        }


      

        private void checkBox4_CheckedChanged_1(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            if (ksrow2 > jsrow2)
            {
                int r;
                r = ksrow2;
                ksrow2 = jsrow2;
                jsrow2 = r;

            }
            for (int i = ksrow2; i <= jsrow2; i++)
            {
                this.dataGridView2.Rows[i].Cells[1].Value = checkBox4.Checked;
                // this.dataGridView2.Rows[i].Cells[1].Value = checkBox1.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;
        }

        private void checkBox3_CheckedChanged_1(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;


            for (int i = 0; i < this.dataGridView2.Rows.Count; i++)
            {
                this.dataGridView2.Rows[i].Cells[1].Value = checkBox3.Checked;

                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }

            this.progressBar1.Value = 100;
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            ksrow = e.RowIndex;
            this.checkBox1.Checked = false;
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            jsrow = e.RowIndex;
            this.checkBox1.Checked = false;
        }

        private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            ksrow2 = e.RowIndex;
            this.checkBox4.Checked = false;
        }

        private void dataGridView2_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            jsrow2 = e.RowIndex;
            this.checkBox4.Checked = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (pub.是否有权限("008002"))
            {
               
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {

                    if (dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString() == "True")
                    {
                        string 工号 = this.dataGridView1.Rows[i].Cells["C工号"].Value.ToString();

                        //先清空当前人员已有的权限
                        string sql2 = "  delete from Z_usergn where 授权人员 = '" + 工号 + "'";
                        pub.ExcuteSql(sql2, "删除权限");
                        //string sql = "  insert into Z_usergn (授权人员) values ('" + 人员 + "')  ";

                        //pub.ExcuteSql(sql, "授权人员");

                        for (int j = 0; j < this.dataGridView2.Rows.Count; j++)
                        {

                            if (dataGridView2.Rows[j].Cells[1].EditedFormattedValue.ToString() == "True")
                            {


                                if (j >= 0)
                                {
                                    string 权限编号 = this.dataGridView2.Rows[j].Cells["D权限编号"].Value.ToString();

                                    string sql3 = "  insert into Z_usergn (授权人员) values ('" + 工号 + "')  ";

                                    pub.ExcuteSql(sql3, "授权人员");

                                    Getid();
                                    string id = Getid().ToString();

                                    string sql1 = "   update Z_usergn set 权限编号='" + 权限编号 + "' where 授权人员='" + 工号 + "' and id= '" + id + "' ";

                                    pub.ExcuteSql(sql1, "权限编号");
                                }
                                else
                                {
                                    string 权限编号 = this.dataGridView2.Rows[j].Cells["D权限编号"].Value.ToString();

                                    Getid();
                                    string id = Getid().ToString();

                                    string sql1 = "   update Z_usergn set 权限编号='" + 权限编号 + "' where 授权人员='" + 工号 + "'  and id= '" + id + "' ";

                                    pub.ExcuteSql(sql1, "权限编号");
                                }
                            }


                        }
                    }


                }
                MessageBox.Show("权限添加成功！", "提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }

        }

        private string Getid()
        {
            string sql = " select max(id) from Z_usergn ";
            System.Data.DataTable tb = pub.U8GetTable(sql);

            if (tb != null)
            {
                if (tb.Rows.Count > 0)
                {

                    return tb.Rows[0][0].ToString();
                }
                else
                {
                    return "";
                }
            }
            else {
                return "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
             if (pub.是否有权限("008003"))
            {
            Form fm = new Formscqx();
            fm.ShowDialog();
            }
             else
             {
                 MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
             }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //如果点击的是表格内
            if (e.RowIndex >= 0)
            {
                //当前行的工号
                string cuser_id = this.dataGridView1.Rows[e.RowIndex].Cells["C工号"].Value.ToString();
                //查出当前工号的权限
                string sql = "select 权限编号 from Z_usergn where 授权人员 = '" + cuser_id + "'";
                System.Data.DataTable tb = pub.U8GetTable(sql);
                if (tb != null)
                {
                    for (int i = 0; i < this.dataGridView2.Rows.Count; i++)
                    {
                        this.dataGridView2.Rows[i].Cells[1].Value = false;
                    }
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        for (int j = 0; j < this.dataGridView2.Rows.Count; j++)
                        {
                            if (this.dataGridView2.Rows[j].Cells["D权限编号"].Value.ToString() == tb.Rows[i][0].ToString())
                            {
                                this.dataGridView2.Rows[j].Cells[1].Value = true;
                            }
                        }
                    }
                }
            }
        }
    }
}
